package com.arvin.controller;

import com.arvin.pojo.Log;
import com.arvin.pojo.PageBean;
import com.arvin.pojo.Result;
import com.arvin.pojo.ScoreInfo;
import com.arvin.service.*;
import com.arvin.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;


@RestController
@RequestMapping("/admin")
@Tag(name = "成绩相关接口",description = "成绩相关接口")
@Slf4j
public class ScoreController {

    @Autowired
    private ScoreService scoreService;
    @Autowired
    private ExamService examService;
    @Autowired
    private InterviewService interviewService;
    @Autowired
    private LogService logService;

    /**
     * 成绩查询
     * @param studentId
     * @param stuName
     * @param className
     * @param page
     * @param pageSize
     * @return
     */
    @Operation(summary = "成绩查询",description = "成绩查询")
    @GetMapping("/getScoreInfo")
    public Result<PageBean> getScoreInfo(String studentId, String stuName,String className, Integer page, Integer pageSize){
        PageBean pageBean = scoreService.getScoreInfo(page,pageSize,studentId,stuName,className);
        log.info("成绩查询信息{}", pageBean);
        return Result.success(pageBean);
    }

    /**
     * 修改成绩
     * @param scoreInfo
     * @return
     */
    @Operation(summary = "修改成绩",description = "修改笔试面试成绩")
    @PutMapping("/updateScore")
    public Result updateScore(@RequestBody ScoreInfo scoreInfo){
        log.info("修改的笔试成绩{}，面试成绩{}",scoreInfo.getExamScore(),scoreInfo.getIntvScore());

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");

        if(!StringUtils.isEmpty(scoreInfo.getExamScore())) {
            examService.updateExamState(scoreInfo.getExamId(),"",scoreInfo.getExamScore());

            // 添加日志
            Log log = new Log();
            log.setUsername(username);
            log.setLogType("考试");
            log.setLogOperate("管理员" + username + "为学号为" + scoreInfo.getStudentId() +
                    "的学生考试打分：" + scoreInfo.getExamScore());
            log.setLogTime(LocalDateTime.now());
            logService.insert(log);
        }
        if(!StringUtils.isEmpty(scoreInfo.getIntvScore())){
            interviewService.updateIntvState(scoreInfo.getIntvId(),"",scoreInfo.getIntvScore());

            // 添加日志
            Log log = new Log();
            log.setUsername(username);
            log.setLogType("面试");
            log.setLogOperate("管理员" + username + "为学号为" + scoreInfo.getStudentId() +
                    "的学生面试打分：" + scoreInfo.getIntvScore());
            log.setLogTime(LocalDateTime.now());
            logService.insert(log);
        }
        return Result.success();
    }
}
